// merchants/records/index.js
const utils = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    tag_index:0,//0全部，1收入，2支出
    recordList: []
  },

  // 查看所有卡及客户的消费记录
  getCardRecordList: function(){
    let that = this;
    let param = {};
    if (that.data.tag_index == 3){
      param = {
        createUid: app.globalData.userInfo.Uid,
        direction: 0,
        cardType: 3,
        page: that.data.CurrentPage
      }
    }else{
      param = {
        createUid: app.globalData.userInfo.Uid,
        direction: that.data.tag_index,
        page: that.data.CurrentPage
      }
    }
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardRecordList',
      data:param,
      method:'POST',
      success: function(res){
        if (res.data.Code==0){
          let recordList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.recordList
            recordList = preGoods.concat(recordList);
          }
          for (let i = 0; i < recordList.length; i++) {
            // let oldExpireTime = utils.jianDate(recordList[i].ExpireTime, recordList[i].Amount);
            let oldExpireTime = utils.formatTime(utils.jianDate(recordList[i].ExpireTime, recordList[i].Amount)).substr(0,10).replace(/\//g, "-");
            recordList[i].oldExpireTime = oldExpireTime;
            recordList[i].ExpireTime = recordList[i].ExpireTime.substr(0, 10);
          }
          that.setData({
            recordList: recordList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCardRecordList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getCardRecordList();
  },
  changeTag: function(e){
    let tag_index = e.currentTarget.dataset.tagindex;
    this.setData({
      tag_index: tag_index
    })
    this.getCardRecordList();
  },
  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getCardRecordList();
    } else {
      return
    }
  },
  
})